<?php

// +----------------------------------------------------------------------
// | ThinkAdmin
// +----------------------------------------------------------------------
// | 版权所有 2014~2017 广州楚才信息科技有限公司 [ http://www.cuci.cc ]
// +----------------------------------------------------------------------
// | 官方网站: http://think.ctolog.com
// +----------------------------------------------------------------------
// | 开源协议 ( https://mit-license.org )
// +----------------------------------------------------------------------
// | github开源项目：https://github.com/zoujingli/ThinkAdmin
// +----------------------------------------------------------------------

namespace app\stage\controller;
use controller\BasicAdmin;
use service\DataService;
use service\ToolsService;
use think\Db;

class Pinben extends BasicAdmin
{

    /**
     * 指定当前数据表
     * @var string
     */
    public $table = 'php_pinben';

    /**
     * 商品列表
     * @return array|string
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @throws \think\Exception
     */
    public function index()
    {
        $this->title = '拼本列表';
        list($get, $db) = [$this->request->get(), Db::name($this->table)];
        foreach (['pname'] as $key) {
            (isset($get[$key]) && $get[$key] !== '') && $db->whereLike($key, "%{$get[$key]}%");
        }
        return parent::_list($db->order('status add'));
    }


    public function add()
    {
        $this->title = '添加拼本';
        $result = Db::table('php_hshop')->select();
        $this->assign('data',$result);
        return $this->_form($this->table, 'form');
    }
    /**
     * 编辑商家
     * @return array|string
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @throws \think\Exception
     */
    public function edit()
    {
        $shuxin = $_GET['shuxin'];
        $this->title = '编辑拼本';
        $result = Db::table('php_hshop')->select();

        //得到属性字段 拆分为数组，循环查询得到值。
        $shuxin = explode(',',$shuxin);
        $datas = array();
        foreach ($shuxin as $k=>$v){
            $datashuxun = Db::table('php_pinben_shuxin')->where(['id' => $v])->find();
            $datas[] = $datashuxun['title'];
        }
        $this->assign('shuxin',$datas);
        $this->assign('data',$result);
        return $this->_form($this->table, 'form');
    }

    /**
     * 删除商家
     * @throws \think\Exception
     * @throws \think\exception\PDOException
     */
    public function del()
    {
        if (DataService::update($this->table)) {
            $this->success("删除成功！", '');
        }
        $this->error("删除失败，请稍候再试！");

    }

    /**
     * 商家禁用
     * @throws \think\Exception
     * @throws \think\exception\PDOException
     */
    public function forbid()
    {

        if (DataService::update($this->table)) {
            $this->success("下架成功！", '');
        }
        $this->error("下架失败，请稍候再试！");
    }

    /**
     * 商家启用
     * @throws \think\Exception
     * @throws \think\exception\PDOException
     */
    public function resume()
    {
        if (DataService::update($this->table)) {
            $this->success("上架成功！", '');
        }
        $this->error("上架失败，请稍候再试！");
    }

    /**
     * 添加成功回跳处理
     * @param bool $result
     */
    protected function _form_result($result)
    {
        if ($result !== false) {
            list($base, $spm, $url) = [url('@admin'), $this->request->get('spm'), url('stage/pinben/index')];
            $this->success('数据保存成功！', "{$base}#{$url}?spm={$spm}");
        }
    }
}
